package com.example.app2024.sensor;

import android.content.pm.ActivityInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.app2024.R;
import com.example.app2024.aidl.Person;

/**
 * 1 获取sensormanager
 * 2 获取 sensor对象
 * 3 注册 sensor 对象
 * 4 重写  onAccuracychanged   onSensorChanged这二个方法
 * 5 注销 sensor 对象
 */
public class SensorActivity extends AppCompatActivity implements SensorEventListener
{
      //fixme 试试看
      //todo
      private ImageView mImageView;
      private SensorManager mSensorManager;
      private Sensor mSensor;
      private float mStartDegree = 0f;

      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sensor_manager);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

            mImageView = (ImageView) findViewById(R.id.imageView);

            mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

            mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);

      }

      @Override
      protected void onResume()
      {
            super.onResume();
            mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_GAME);
      }

      @Override
      protected void onPause()
      {
            super.onPause();
            mSensorManager.unregisterListener(this);
      }

      @Override
      public void onSensorChanged(SensorEvent event)
      {
            //不要阻塞
            if (event.sensor.getType() == Sensor.TYPE_ORIENTATION)
            {
                  float degree = -event.values[0];

                  RotateAnimation rotateAnimation = new RotateAnimation(mStartDegree, degree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

                  rotateAnimation.setDuration(300);
                  mImageView.startAnimation(rotateAnimation);
                  mStartDegree = degree;
                  Person person = new Person();

            }
      }

      @Override
      public void onAccuracyChanged(Sensor sensor, int accuracy)
      {

      }
}